package com.tiny_wind.tiny_wind_diary.controller;

import com.tiny_wind.tiny_wind_diary.entity.NoteEntity;
import com.tiny_wind.tiny_wind_diary.model.TwErrorModel;
import com.tiny_wind.tiny_wind_diary.model.TwResultDataModel;
import com.tiny_wind.tiny_wind_diary.model.TwResultModel;
import com.tiny_wind.tiny_wind_diary.service.impl.DiaryUnitServiceImpl;
import com.tiny_wind.tiny_wind_diary.service.impl.NoteServiceImpl;
import com.tiny_wind.tiny_wind_diary.utility.JsComparisionManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/Note") // url:/模块/资源/{id}/细分 /seckill/list
public class NoteController extends BaseController {
		private Logger logger = LogManager.getLogger(this.getClass());
	
		@Autowired
		private NoteServiceImpl mService;
		@RequestMapping(value = "/Search", method = RequestMethod.POST)
		@ResponseBody
		public TwResultModel<NoteEntity> search(@RequestBody NoteEntity filter) {
			Integer userId = this.getUserId();
			if(filter.userId == null) {
				return new TwResultModel<NoteEntity>(TwErrorModel.InvalidRequestParam());
			}
			if(!JsComparisionManager.isIntegerEq(filter.userId,userId)) {
				return new TwResultModel<NoteEntity>(TwErrorModel.InvalidToken());
			}
//			String info = String.format("Diary filter is; %s", JsJsonManager.getJson(filter));
//			logger.info(info);
			try {
				NoteEntity entity = mService.search(filter).get(0);

				logger.debug("search success, userId:%s,date:%s",userId);
//				Page pageInfo = new Page(filter.page,filter.pageSize,totalCount);
				TwResultModel<NoteEntity> result = new TwResultModel<NoteEntity>(entity);
				return result;
			}
			catch (Exception e) {
				logger.debug("search exception,%s",e.getMessage());
				e.printStackTrace();
				return new TwResultModel<NoteEntity>(-1);
			}
			
		}

	@RequestMapping(value = "/Insert", method = RequestMethod.POST)
	public TwResultModel<NoteEntity> insert(@RequestBody NoteEntity entity) {
		Integer userId = getUserId();
		if(entity.userId == null) {
			return new TwResultModel<NoteEntity>(TwErrorModel.InvalidRequestParam());
		}
		if(!JsComparisionManager.isIntegerEq(entity.userId,userId)) {
			return new TwResultModel<NoteEntity>(TwErrorModel.InvalidToken());
		}
		try {
			// 返回值是一个bool值,应该是返回影响的条数.新增的id直接在entity中.
			mService.insert(entity);
			logger.debug("Insert entity success");
			TwResultDataModel<NoteEntity> data = new TwResultDataModel<NoteEntity>(entity);
			TwResultModel<NoteEntity> result = new TwResultModel<NoteEntity>(data);
			return result;
		}
		catch (DuplicateKeyException e) {
			// 主键冲突
			return new TwResultModel<NoteEntity>(TwErrorModel.DuplicateInsert());
		}
		catch (Exception e) {
			logger.debug("Insert exception,%s",e.getMessage());
			e.printStackTrace();
			return new TwResultModel<NoteEntity>(TwErrorModel.UnhandledError());
		}
	}


	//null代表没有初始值,不应该更新为
	@RequestMapping(value = "/Update", method = RequestMethod.POST)
	public TwResultModel<NoteEntity> update(@RequestBody NoteEntity entity) {
		Integer userId = getUserId();
		if(entity.userId == null) {
			return new TwResultModel<NoteEntity>(TwErrorModel.InvalidRequestParam());
		}
		if(!JsComparisionManager.isIntegerEq(entity.userId,userId)) {
			return new TwResultModel<NoteEntity>(TwErrorModel.InvalidToken());
		}
		try {
			// 返回值是一个bool值,应该是返回影响的条数.新增的id直接在entity中.
			mService.update(entity);
			TwResultModel<NoteEntity> result = new TwResultModel<NoteEntity>(entity);
			return result;
		} catch (Exception e) {
			logger.debug("Update exception,%s",e.getMessage());
			e.printStackTrace();
			return new TwResultModel<NoteEntity>(-1);
		}
	}
}